/*
 * Spring 2012 TCSS360 Deliverable 3
 * Matthew Selfors
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.Bidder;
import model.Item;
import model.User;
import exceptions.BidLessThanMinBidException;
import exceptions.BidderNotRegisteredException;
import exceptions.DuplicateBidException;

@SuppressWarnings("serial")
public class ItemGUI extends JPanel {

	private Item item;

	private JFrame frame;

	private JPanel item_details;

	private JTextArea item_comments;

	private JTextField bid;

	private JButton place_bid;

	private User user;

	public ItemGUI(final Item the_item, final User the_user) {
		super();

		item = the_item;
		frame = new JFrame();
		setLayout(new BorderLayout());
		user = the_user;
		setBackground(Color.WHITE);

		setUpItemDetails();

		frame.setResizable(false);
		frame.setPreferredSize(new Dimension(600, 400));
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.add(this);
		frame.pack();
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
	}

	private void setUpItemDetails() {
		Font font = new Font("itemDetails", 0, 18);
		JLabel item_name =
				new JLabel("Item name: "
						+ item.getItemName());
		JLabel item_id =
				new JLabel("Item ID: " + item.getID());
		JLabel npo = new JLabel("NPO: " + item.getNPO());
		JLabel min_bid =
				new JLabel("Minimum bid: "
						+ item.getMinBid().toString());
		JLabel condition =
				new JLabel("Condition: "
						+ item.getCondition());
		JLabel quantity =
				new JLabel("Quantity: "
						+ item.getQuantity());
		JLabel comments = new JLabel("Comments: ");

		item_name.setFont(font);
		item_id.setFont(font);
		npo.setFont(font);
		min_bid.setFont(font);
		condition.setFont(font);
		quantity.setFont(font);
		comments.setFont(font);

		item_comments = new JTextArea(item.getComment());
		item_comments.setFont(font);
		item_comments.setEditable(false);
		JScrollPane scroll = new JScrollPane(item_comments);
		scroll.setPreferredSize(new Dimension(80, 100));

		JPanel north_panel = new JPanel(new BorderLayout());
		north_panel.setBackground(Color.WHITE);

		item_details = new JPanel(new GridLayout(6, 1));
		item_details.setBackground(Color.WHITE);
		JPanel item_comments_panel = new JPanel();
		item_comments_panel.setLayout(new GridLayout(2, 1));
		item_details.add(item_name);
		item_details.add(item_id);
		item_details.add(npo);
		item_details.add(min_bid);
		item_details.add(condition);
		item_details.add(quantity);

		JPanel bid_panel = new JPanel();
		bid_panel.setBackground(Color.WHITE);
		bid_panel.setLayout(new BoxLayout(bid_panel,
				BoxLayout.PAGE_AXIS));

		if (user.getAccessLevel() == 3) {
			bid = new JTextField();
			bid.setMaximumSize(new Dimension(218, 30));
			bid.setFont(font);
			bid_panel.add(bid);
			place_bid = new JButton("Place bid");
			place_bid.addActionListener(new BidListener());
			place_bid.setFont(font);
			bid_panel.add(place_bid);
		}

		north_panel.add(item_details, BorderLayout.WEST);
		north_panel.add(bid_panel, BorderLayout.EAST);
		add(north_panel, BorderLayout.NORTH);

		item_comments_panel.setBackground(Color.WHITE);
		item_comments_panel.add(comments);
		item_comments_panel.add(scroll);
		add(item_comments_panel, BorderLayout.SOUTH);
	}

	private class BidListener implements ActionListener {

		@Override
		public void actionPerformed(
				final ActionEvent the_event) {
			try {
				BigDecimal bid =
						new BigDecimal(
								ItemGUI.this.bid.getText());

				int value =
						JOptionPane
								.showConfirmDialog(
										ItemGUI.this,
										"Are you sure you want to bid on this item?",
										"Confirmation",
										JOptionPane.YES_NO_OPTION,
										JOptionPane.QUESTION_MESSAGE);
				if (value == JOptionPane.YES_OPTION) {
					try {
						ItemGUI.this.item
								.placeBid(
										bid,
										((Bidder) ItemGUI.this.user));
						JOptionPane.showMessageDialog(
								ItemGUI.this,
								"Bid placed successfully.",
								"Congratulations!",
								JOptionPane.PLAIN_MESSAGE);
					} catch (BidLessThanMinBidException e) {
						JOptionPane
								.showMessageDialog(
										ItemGUI.this,
										"Bid must be higher than minimum bid.",
										"Invalid bid.",
										JOptionPane.ERROR_MESSAGE);
					} catch (BidderNotRegisteredException e) {
						JOptionPane
								.showMessageDialog(
										ItemGUI.this,
										"Must be a registered bidder.",
										"Invalid user.",
										JOptionPane.ERROR_MESSAGE);
					} catch (DuplicateBidException e) {
						JOptionPane
								.showMessageDialog(
										ItemGUI.this,
										"Bid has already been placed for this item.",
										"Invalid bid.",
										JOptionPane.ERROR_MESSAGE);
					}
				}
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(ItemGUI.this,
						"Must enter a real number.",
						"Invalid bid.",
						JOptionPane.ERROR_MESSAGE);
			}
			ItemGUI.this.bid.setText("");
		}
	}
}
